import com.adobe.cairngorm.control.CairngormEventDispatcher;

import flash.display.DisplayObject;

import mx.controls.Alert;
import mx.events.ModuleEvent;
import mx.modules.IModuleInfo;
import mx.modules.ModuleManager;

import net.optix.ip.events.GetBidInventionsEvent;
import net.optix.ip.events.GetClientsEvent;
import net.optix.ip.events.GetCopyrightActionsEvent;
import net.optix.ip.events.GetCopyrightsEvent;
import net.optix.ip.events.GetFirmMattersEvent;
import net.optix.ip.events.GetFirmsEvent;
import net.optix.ip.events.GetInventionsEvent;
import net.optix.ip.events.GetInventorsEvent;
import net.optix.ip.events.GetMattersEvent;
import net.optix.ip.events.GetPractitionersEvent;
import net.optix.ip.events.GetTrackInventionsByFirmEvent;
import net.optix.ip.events.GetTrackInventionsEvent;
import net.optix.ip.events.GetTradeMarksEvent;
import net.optix.ip.events.LogOutEvent;
import net.optix.ip.model.ModelLocator;

import views.ViewInstance;

[Bindable]
private var model:ModelLocator	=	ModelLocator.getInstance();

private var _view:int;

public function set view(value:int):void
{
	_view	=	value;
	var userID:int;
	try
	{
		switch(model.role)
		{
			case 1:
				userID	=	model.client.clientID;
				break;
			case 2:
				userID	=	model.inventor.inventorID;
				break;
			case 3:
				userID	=	model.firm.firmID;
				break;
			case 4:
				userID	=	model.practitioner.practitionerID;
				break;
		}
	}
	catch(error:Error){}
	switch(value)
	{
		case ViewInstance.Welcome:
			loadModule("views/welcome/Welcome.swf");
			break;
		case ViewInstance.ViewInventors:
			try
			{
				var getInventorsEvent:GetInventorsEvent	=	new GetInventorsEvent(model.client.clientID);
				com.adobe.cairngorm.control.CairngormEventDispatcher.getInstance().dispatchEvent(getInventorsEvent);
				loadModule("views/user/InventorsList.swf");
			}
			catch(error:Error){trace(error);}
			break;
		case ViewInstance.ViewBids:
			var userID:int;
			try
			{
				userID	=	model.firm.firmID;
			}
			catch(error:Error)
			{
				userID	=	0;
			}
			var getBidInventions:GetBidInventionsEvent	=	new GetBidInventionsEvent(userID);
			com.adobe.cairngorm.control.CairngormEventDispatcher.getInstance().dispatchEvent(getBidInventions);
			loadModule("views/invention/BidInventionsList.swf");
			break;
		case ViewInstance.ViewInventions:
			var getInventions:GetInventionsEvent	=	new GetInventionsEvent(model.role , userID);
			com.adobe.cairngorm.control.CairngormEventDispatcher.getInstance().dispatchEvent(getInventions);
			loadModule("views/invention/InventionsList.swf");
			break;
		case ViewInstance.ViewInvention:
			loadModule("views/invention/ViewInvention.swf");
			break;
		case ViewInstance.LogOut:
			var logout:LogOutEvent	=	new LogOutEvent();
			com.adobe.cairngorm.control.CairngormEventDispatcher.getInstance().dispatchEvent(logout);
			break;
		case ViewInstance.ViewPractitioners:
			var getPractitioners:GetPractitionersEvent	=	new GetPractitionersEvent(model.firm.firmID);
			com.adobe.cairngorm.control.CairngormEventDispatcher.getInstance().dispatchEvent(getPractitioners);
			loadModule("views/user/PractitionersList.swf");
			break;
		case ViewInstance.ViewClients:
			var getClientsEvent:GetClientsEvent	=	new GetClientsEvent();
			com.adobe.cairngorm.control.CairngormEventDispatcher.getInstance().dispatchEvent(getClientsEvent);
			loadModule("views/manager/ClientsList.swf");
			break;
		case ViewInstance.ViewFirms:
			var getFirmsEvent:GetFirmsEvent	=	new GetFirmsEvent();
			com.adobe.cairngorm.control.CairngormEventDispatcher.getInstance().dispatchEvent(getFirmsEvent);
			loadModule("views/manager/FirmsList.swf");
			break;
		case ViewInstance.TrackInventions:	
			try
			{
				var getTrackInventions:GetTrackInventionsEvent	=	new GetTrackInventionsEvent(model.client.clientID);
				CairngormEventDispatcher.getInstance().dispatchEvent(getTrackInventions);
				/*var getFirmsWork:GetFirmsWorkEvent = new GetFirmsWorkEvent(model.client.clientID);
				CairngormEventDispatcher.getInstance().dispatchEvent(getFirmsWork);*/
				var getTrackInventionsByFirm:GetTrackInventionsByFirmEvent	=	
					new GetTrackInventionsByFirmEvent(model.client.clientID);
				CairngormEventDispatcher.getInstance().dispatchEvent(getTrackInventionsByFirm);	
			}
			catch(error:Error){}
			loadModule("views/client/TrackInventions.swf");	
			break;
		case ViewInstance.ViewTradeMarks:	
			var getmark:GetTradeMarksEvent	=	new GetTradeMarksEvent(userID,model.role);
			CairngormEventDispatcher.getInstance().dispatchEvent(getmark);
			loadModule("views/mark/TradeMark.swf");	
			break;
		case ViewInstance.ViewCopyrights:	
			var getcopyrights:GetCopyrightsEvent	=	new GetCopyrightsEvent(userID,model.role);
			CairngormEventDispatcher.getInstance().dispatchEvent(getcopyrights);
			loadModule("views/copyright/Copyright.swf");	
			break;
		case ViewInstance.ViewTradeMark:
				loadModule("views/mark/ViewTradeMark.swf");	
			break;
		case ViewInstance.TradeSecret:
			loadModule("views/client/TradeSecret.swf");
			break;
		case ViewInstance.ViewCopyRight:
			try
			{		
				var getCopyrightActions:GetCopyrightActionsEvent	=	new GetCopyrightActionsEvent(model.currentCopyright.copyrightID);
				CairngormEventDispatcher.getInstance().dispatchEvent(getCopyrightActions);
			}
			catch(error:Error)
			{
				trace(error.getStackTrace());
			}
			
			loadModule("views/copyright/ViewCopyright.swf");	
			break;
		case ViewInstance.ViewTradeMarkBids:
			loadModule("views/mark/BidTradeMarks.swf");
			break;
		case ViewInstance.ViewCopyrightBids:
			loadModule("views/copyright/BidCopyrights.swf");
			break;
		case ViewInstance.FirmMatters:
			try
			{
				var getFirmMatters:GetFirmMattersEvent	=	new GetFirmMattersEvent(model.firm.firmID);
				com.adobe.cairngorm.control.CairngormEventDispatcher.getInstance().dispatchEvent(getFirmMatters);
				
				var getMatters:GetMattersEvent	=	new GetMattersEvent(model.firm.firmID);
				com.adobe.cairngorm.control.CairngormEventDispatcher.getInstance().dispatchEvent(getMatters);
			}
			catch(error:Error)
			{
				trace(error.getStackTrace());
			}
			loadModule("views/firm/FirmMatters.swf");
			break;
	}
}

public function get view():int
{
	return _view;
}

protected var _moduleInfo:IModuleInfo;

private function getModuleInfo():IModuleInfo
{
	return _moduleInfo;
}

public function loadModule(module:String):void
{
	_moduleInfo = ModuleManager.getModule(module);
    _moduleInfo.addEventListener(ModuleEvent.READY, onModuleReady);
    _moduleInfo.addEventListener(ModuleEvent.SETUP, onModuleSetup);
    _moduleInfo.addEventListener(ModuleEvent.UNLOAD, onModuleUnload);
    _moduleInfo.addEventListener(ModuleEvent.PROGRESS, onModuleProgress);
    _moduleInfo.addEventListener(ModuleEvent.ERROR, onModuleError);           
    getModuleInfo().load();
}

protected function onModuleProgress (event:ModuleEvent) : void 
{
	var proc: uint = event.bytesLoaded / event.bytesTotal * 100;
	
	model.loadProcess	=	proc;
}

private var dis:DisplayObject;

protected function onModuleError (e:ModuleEvent) : void 
{
	Alert.show(e.errorText,'error',1,null);
}

private function onModuleSetup (e:ModuleEvent) : void 
{
	
}

private function onModuleReady (e:ModuleEvent):void 
{
	var moduleInfo:IModuleInfo = e.currentTarget as IModuleInfo;
   	try
	{
		this.removeAllChildren();
		this.addChild(	moduleInfo.factory.create () as DisplayObject );
	}
	catch(error:Error)
	{
		
	}
}

public function onModuleUnload (e:ModuleEvent) : void 
{
	         
}

public function displayModule(d:DisplayObject):void
{
	
	
}